package com.donleo.ssm.controller;

import com.donleo.ssm.model.RoleModel;
import com.donleo.ssm.service.IRoleService;
import com.donleo.ssm.utils.ResponseLayResult;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @author liangd
 * date 2020-11-24 15:17
 * code
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    @Resource
    private IRoleService roleService;

    /**
     *
     * 新增
     */
    @PostMapping("/add")
    public ResponseLayResult add(@RequestBody RoleModel roleModel){
        Integer count = roleService.add(roleModel);
        return ResponseLayResult.createBySuccess(count,count);
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public ResponseLayResult delete(Integer id){
        Integer count = roleService.delete(id);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PostMapping("/deletes")
    public ResponseLayResult deletes(Integer[] ids){
        List<Integer> idList = Arrays.asList(ids);
        Integer count = roleService.deletes(idList);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 修改
     * @param roleModel
     * @return
     */
    @PostMapping("/update")
    public ResponseLayResult update(@RequestBody RoleModel roleModel){
        Assert.notNull(roleModel.getId(),"Id is not null");
        Integer count = roleService.update(roleModel);
        return ResponseLayResult.createBySuccess(count,count);
    }

    /**
     * 查询单个
     * @param id
     * @return
     */
    @GetMapping("/findById")
    public ResponseLayResult findById(Integer id){
        RoleModel RoleModel = roleService.findById(id);
        return ResponseLayResult.createBySuccess(1,RoleModel);
    }

    /**
     * 查询所有
     * @param roleModel
     * @return
     */
    @GetMapping("/findAll")
    public ResponseLayResult findAll(RoleModel roleModel){
        List<RoleModel> list = roleService.findAll(roleModel);
        Integer count = roleService.getCount(roleModel);
        return ResponseLayResult.createBySuccess(count,list);
    }
}

